﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Xml;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.ServiceProcess;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA
{
    partial class ServerSqlConnSet : Form
    {
        public ServerSqlConnSet()
        {
            InitializeComponent();
        }
        private void ServerSqlConnSet_Load(object sender, EventArgs e)
        {
            #region GetXML
            if (File.Exists("SytemConfig.xml"))
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(Application.StartupPath + "\\SytemConfig.xml");
                foreach (XmlNode item in xmldoc.GetElementsByTagName("DOC"))
                {
                    foreach (XmlNode items in item.ChildNodes)
                    {
                        if (items.Name == "APPLICATIONPROGRAMIP")
                            this.txtEWMSAServer_IP.Text = items.InnerText.Trim();
                        else if (items.Name == "APPLICATIONPROGRAMDB")
                            this.txtEWMSA_DBName.Text = items.InnerText.Trim();
                        else if (items.Name == "APPLICATIONPROGRAMUR")
                            this.txtEWMSA_DBuser.Text = items.InnerText.Trim();
                        else if (items.Name == "APPLICATIONPROGRAMPD")
                              if (items.InnerText.Trim() != "")
                                  this.txtEWMSA_DBPWD.Text = ElectronicWMSA.Comm.ClassFiles.PCInfoClass.Decryption(items.InnerText.Trim(), "/*-./.,@");
                            else
                                  this.txtEWMSA_DBPWD.Text = "";
                      
                    }
                }
            }
            #endregion
        }

        private void btnSQLConnection_Click(object sender, EventArgs e)
        {
            string _IP = this.txtEWMSAServer_IP.Text.Trim();
            string _DBName = this.txtEWMSA_DBName.Text.Trim();
            string _user = this.txtEWMSA_DBuser.Text.Trim();
            string _pwd = this.txtEWMSA_DBPWD.Text.Trim();
            try
            {
                if (_IP == "")
                {
                    MessageClass.error("不能为空！"); this.txtEWMSAServer_IP.Focus(); return;
                }
                if (_DBName == "")
                {
                    MessageClass.error("不能为空！"); this.txtEWMSA_DBName.Focus(); return;
                }
                if (_user == "")
                {
                    MessageClass.error("不能为空！"); this.txtEWMSA_DBuser.Focus(); return;
                }
                if (_pwd == "")
                {
                    MessageClass.error("不能为空！"); this.txtEWMSA_DBPWD.Focus(); return;
                }

                if (MessageClass.enquire("保存设置参数！") == DialogResult.OK)
                {
                    XmlDocument doc = new XmlDocument();
                    string strFileName = Application.StartupPath + "\\SytemConfig.xml";
                    doc.Load(strFileName);
                    XmlNodeList nodeList = doc.SelectSingleNode("DOC").ChildNodes;
                    foreach (XmlNode xn in nodeList)
                    {
                        if (xn.Name == "APPLICATIONPROGRAMIP")
                            xn.InnerText = _IP;
                        else if (xn.Name == "APPLICATIONPROGRAMDB")
                            xn.InnerText = _DBName;
                        else if (xn.Name == "APPLICATIONPROGRAMUR")
                            xn.InnerText = _user;
                        else if (xn.Name == "APPLICATIONPROGRAMPD")
                            xn.InnerText = _pwd;
                    }
                    doc.Save(strFileName);
                    MessageClass.right("设置成功，程序将重启！");
                    Application.Exit();
                    System.Diagnostics.Process.Start(Application.ExecutablePath);
                }
            }
            catch (Exception)
            {
                MessageClass.error("错误！");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 移动
        private void txtServer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.btnSQLConnection_Click(sender, e);
            }
        }

        Point downPoint;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = new Point(e.X, e.Y);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }
        #endregion

    }
}
